



//
//  NAuthMessageViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/14.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NAuthMessageViewController.h"
#import "NDataCell.h"
#import "NDataChangeViewController.h"
#import "NAuthModel.h"
#import "NFillDataViewController.h"

@interface NAuthMessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSArray *titles;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)NSArray *msgs;

@property (nonatomic,copy)NSString *status;
///执照和副本的url
@property (nonatomic,copy)NSString *picUrl;
@property (nonatomic,copy)NSString *picCopyUrl;

@property (nonatomic,strong)UIButton *authAgainBtn;


@end
#define cellIdentifier @"datacell"
@implementation NAuthMessageViewController

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NDataCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.authAgainBtn.hidden = YES;
    [self.authAgainBtn removeFromSuperview];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    self.dataArray = [NSMutableArray array];
    [self.navigationItem setItemWithTitle:@"认证信息" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    if ([[NUserModel shareInstance].userInfoType isEqualToString:@"1"]) {
         self.titles = @[@"认证状态",@"企业名称",@"统一社会信用代码",@"联系人姓名",@"联系人电话",@"",@""];
    }else{
         self.titles = @[@"认证状态",@"姓名",@"身份证号",@"联系人电话",@"",@"",@""];
    }
   
    
    //0 审核中 1已认证  2未通过
    
    [self.view addSubview:self.tableView];
    
//    [self addFooter];
//    [self loadData];
    // Do any additional setup after loading the view from its nib.
}

//底部填写修改的按钮
- (void)addFooter {
    
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100.f)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 30, 300, 50);
    btn.centerX = footer.centerX;
    btn.backgroundColor = ThemeBlue;
    btn.layer.cornerRadius = 5.f;
    btn.layer.masksToBounds = YES;
    [btn setTitle:@"修改联系人信息" forState:UIControlStateNormal];
    [btn addTarget: self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:btn];
    self.tableView.tableFooterView = footer;
    
}

///重新认证按钮添加
- (void)authAgain{
    
//    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 40, 40) title:@"" Image:nil selectedIMG:nil tartget:self action:@selector(newAuth)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth-100.f, 3.f, 90.f, 40.f);
    //titleLabel 是只读属性
//    btn.titleLabel.textColor = kBlackColor;
    [btn setTitleColor:kBlackColor forState:UIControlStateNormal];
    [btn setTitle:@"重新认证" forState:UIControlStateNormal];
    btn.titleLabel.font = kFont16;
    [btn addTarget:self action:@selector(newAuth) forControlEvents:UIControlEventTouchUpInside];
    self.authAgainBtn = btn;
    [self.navigationController.navigationBar addSubview:btn];
    
}

#pragma mark -
///重新认证
- (void)newAuth{
    
    NFillDataViewController *data = [NFillDataViewController new];
    data.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:data animated:YES];
//    [self.navigationController popViewControllerAnimated:YES];
    //跳转到指定页面
//    NSArray * ctrlArray = self.navigationController.viewControllers;
//    [self.navigationController popToViewController:ctrlArray[1] animated:YES];
    
}

- (void)loadData{
    
    NSDictionary *dic = @{@"identUserId":[NUserModel shareInstance].userInfoId?[NUserModel shareInstance].userInfoId:@"",};
    [[NNRCNetRequest manager]getWithUrl:kAuthMsgInqueryUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        WeakStament(ws);
        
                if ([[result objectForKey:@"code"] integerValue] == 0) {
                    
                    NSDictionary *dict = result[@"page"];

                    [ws formDataWithDict:dict];
                    
//                    for (NSDictionary *dict in arr) {
//                        NAuthModel *model = [NAuthModel mj_objectWithKeyValues:dict];
//                        [ws.dataArray addObject:model];
//                    }
//                    [ws.dataArray addObject:@""];
//                    [ws.dataArray addObject:@""];
                    
                    [ws.tableView reloadData];
                    
                }else{
                    [AFToast showText:result[@"msg"]];
                }
    } failure:^(id failure) {
        
    }];
}

- (void)formDataWithDict:(NSDictionary *)dict {
    
    NSString *status = dict[@"identStatus"];
    
#warning 测试数据记得关闭
    //测试
//    status = @"2";
    
    if ([status integerValue]==0) {
        //
        self.tableView.tableFooterView = nil;
        status = @"审核中";
        self.status = status;
        
    }else if ([status integerValue]==1){
         status = @"已认证";
        [self addFooter];
        self.status = status;
    }else{
        self.tableView.tableFooterView = nil;
        //审核未通过需要重新认证
        [self authAgain];
         status = @"审核未通过";
        self.status = status;
    }
     NSString *qiyeName = dict[@"identEnterpriseName"];
     NSString *unicode = dict[@"identEnterpriseCode"];
     NSString *contact = dict[@"identContact"];
     NSString *tel = dict[@"identTelephone"];
    ///营业执照原件
    self.picUrl = dict[@"identPictureSource"];
    
    ///营业执照复印件
    self.picCopyUrl = dict[@"identPictureCopy"];
//    NSString *status = dict[@"identStatus"];
//     NSString *status = dict[@"identStatus"];
    if ([[NUserModel shareInstance].userInfoType isEqualToString:@"1"]) {
        
        self.msgs = @[status,qiyeName,unicode,contact,tel,@"",@""];
        
    }else{
        
        self.msgs = @[status,qiyeName,unicode,tel,@"",@"",@""];
        
    }
    
}

#pragma mark - 修改联系人信息
- (void)tapAction:(UIButton *)btn
{
    NDataChangeViewController *change = [NDataChangeViewController new];
    change.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:change animated:YES];
    
}

#pragma mark - delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([[NUserModel shareInstance].userInfoType isEqualToString:@"1"]) {
        if (indexPath.row>4) {
            return 125.f;
        }else{
            return 60.f;
        }
    }else{
        ///个人端
        if (indexPath.row>3) {
            return 125.f;
        }else{
            return 60.f;
        }
    }
   
    return 0;
}



#pragma mark - datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    NDataCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
     if ([[NUserModel shareInstance].userInfoType isEqualToString:@"1"]) {
         cell.msgLabl.hidden = false;
         cell.dataTF.hidden = YES;
         cell.titleLabl.text = self.titles[indexPath.row];
         cell.msgLabl.text = self.msgs[indexPath.row];
         if (indexPath.row==0) {
             if ([self.status isEqualToString:@"审核中"]) {
                 cell.msgLabl.textColor = ThemeBlue;
             }else if ([self.status isEqualToString:@"审核未通过"]){
                 cell.msgLabl.textColor = [UIColor redColor];
             }else{
                 //绿色
                 cell.msgLabl.textColor = NColorFrom16RGB(0x49cc93);
             }
         }
         if (indexPath.row>4) {
             cell.msgLabl.hidden = YES;
         }
         if (indexPath.row==5||indexPath.row==6) {
             [self setUIWithCell:cell withIndexPath:indexPath];
         }
     }else{
         
         ///个人端
         cell.msgLabl.hidden = false;
         cell.dataTF.hidden = YES;
         cell.titleLabl.text = self.titles[indexPath.row];
         cell.msgLabl.text = self.msgs[indexPath.row];
         if (indexPath.row==0) {
             if ([self.status isEqualToString:@"审核中"]) {
                 cell.msgLabl.textColor = ThemeBlue;
             }else if ([self.status isEqualToString:@"审核未通过"]){
                 cell.msgLabl.textColor = [UIColor redColor];
             }else{
                 //绿色
                 cell.msgLabl.textColor = NColorFrom16RGB(0x49cc93);
             }
         }
         
         if (indexPath.row>3) {
             cell.msgLabl.hidden = YES;
         }
         if (indexPath.row==4||indexPath.row==5||indexPath.row==6) {
             [self setUIWithCell:cell withIndexPath:indexPath];
         }
         
     }
    
    
    //底部线条
//    UIView *line;
//    if ([[NUserModel shareInstance].userInfoType isEqualToString:@"1"]) {
//
//         line = [[UIView alloc]initWithFrame:CGRectMake(5,indexPath.row>4?124:60, ScreenWidth-10, 0.5)];
//
//    }else{
//
//         line = [[UIView alloc]initWithFrame:CGRectMake(5,indexPath.row>3?124:60, ScreenWidth-10, 0.5)];
//    }
//
//    line.backgroundColor = kGrayColor;
//    [cell addSubview:line];
    return cell;
}


///设置cell 内容
- (void)setUIWithCell:(NDataCell *)cell withIndexPath:(NSIndexPath *)indexPath{
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(23,15,200,18)];
    if ([[NUserModel shareInstance].userInfoType isEqualToString:@"1"]) {
        if (indexPath.row==5) {
            label.text =@"营业执照原件";
        }else{
            label.text =@"营业执照复印件(加盖公章)";
        }
    }else{
        if (indexPath.row==4) {
            label.text =@"身份证原件正面";
        }else if (indexPath.row==5){
             label.text =@"身份证原件背面";
        }
        else{
            label.text =@"手持身份证半身照";
        }
    }
   
    label.textColor = kDarkGrayColor;
    label.font = kFont15;
    [cell.contentView addSubview:label];
    [self addPhotoAreaOnCell:cell withIndexPath:(NSIndexPath *)indexPath];
}

///添加照片选择区域
- (void)addPhotoAreaOnCell:(NDataCell *)cell  withIndexPath:(NSIndexPath *)indexPath{

    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(22, 45, 60, 60)];
    if (indexPath.row==5) {
        if (self.picUrl.length>0) {
            [imageV sd_setImageWithURL:[NSURL URLWithString:self.picUrl]];
        }else{
          imageV.image = WDImgName(@"处理中");
        }
        
    }else{
        if (self.picCopyUrl.length>0) {
            [imageV sd_setImageWithURL:[NSURL URLWithString:self.picCopyUrl]];
        }else{
            imageV.image = WDImgName(@"流程");
        }
        
    }
    
    [cell.contentView addSubview:imageV];
}


@end
